﻿using System;
using System.Data;
using System.Collections.Generic;
using TCKQKL.Service.DataContract;
using TCKQKL.DAL.IDAL;
using System.Data.OleDb;
using TCKQKL.DAL.Converter;

namespace TCKQKL.DAL.Oracle
{
    public class StocksInfo : IStocksInfo
    {
        private OleDbConnection _internalConnection;
        private OleDbTransaction _internalADOTransaction = null;

        public StocksInfo() { }
        public StocksInfo(OleDbConnection conn, OleDbTransaction trans)
        {
            _internalConnection = conn;
            _internalADOTransaction = trans;
        }
        public void BeginADOTransaction()
        {
            if (_internalConnection.State != System.Data.ConnectionState.Open)
                _internalConnection.Open();
            _internalADOTransaction = _internalConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
        }
        public void RollBackTransaction()
        {
            _internalADOTransaction.Rollback();
            _internalADOTransaction = null;
        }
        public void CommitADOTransaction()
        {
            _internalADOTransaction.Commit();
            _internalADOTransaction = null;
        }
        public void Open(string connString)
        {
            if (_internalConnection == null)
                _internalConnection = new OleDbConnection(connString);
            if (_internalConnection.State != ConnectionState.Open)
                _internalConnection.Open();
        }
        public void Close()
        {
            if (_internalConnection != null && _internalConnection.State != System.Data.ConnectionState.Closed)
                _internalConnection.Close();
        }

        private const string GET_STOCKINFO_BYSYMBOL = "SELECT     BOSC.IRS_STOCKS_VIEW.CODE, BOSC.IRS_STOCKS_VIEW.NAME, BOSC.STOCKS_INFO_VIEW.BASIC_PRICE, BOSC.STOCKS_INFO_VIEW.OPEN_PRICE, BOSC.STOCKS_INFO_VIEW.CLOSE_PRICE, BOSC.STOCKS_INFO_VIEW.CURRENT_PRICE, BOSC.STOCKS_INFO_VIEW.HIGHEST_PRICE, BOSC.STOCKS_INFO_VIEW.LOWEST_PRICE, BOSC.STOCKS_INFO_VIEW.UPPER_PRICE, BOSC.STOCKS_INFO_VIEW.LOWER_PRICE, BOSC.STOCKS_INFO_VIEW.STATUS_LISTING FROM         BOSC.IRS_STOCKS_VIEW INNER JOIN BOSC.STOCKS_INFO_VIEW ON BOSC.IRS_STOCKS_VIEW.STOCK_ID = BOSC.STOCKS_INFO_VIEW.STOCK_ID WHERE   BOSC.STOCKS_INFO_VIEW.CODE=?";
        private const string PARM_STOCKCODE = "CODE";

        public StocksDataModel getStockInfoBySymbol(string symbol)
        {
            OleDbDataReader rdr = null;
            try
            {
                OleDbParameter parm1 = new OleDbParameter(PARM_STOCKCODE, OleDbType.VarChar, 8);
                parm1.Value = symbol.ToUpper();

                rdr = OleDbHelper.ExecuteReaderSingleRowSingleParm(_internalConnection, _internalADOTransaction, CommandType.Text, GET_STOCKINFO_BYSYMBOL, parm1);
                if (rdr.HasRows)
                {
                    rdr.Read();
                    return new StocksDataModel(rdr["CODE"].ToString(), VNConverter.TCVN3ToUnicode(rdr["NAME"].ToString()), 0, 0, 0, 0, 0, 0, Convert.ToDecimal(rdr["UPPER_PRICE"]), Convert.ToDecimal(rdr["LOWER_PRICE"]), (Convert.ToDecimal(rdr["UPPER_PRICE"]) == 0) ? 0 : (Convert.ToDecimal(rdr["UPPER_PRICE"]) + Convert.ToDecimal(rdr["LOWER_PRICE"])) / 2, (Convert.ToInt32(rdr["STATUS_LISTING"]) == 1) ? "HOSE" : "HASTC");
                }
                else
                    return null;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                rdr.Close();
            }
        }

    }
}
